/*
 * @Author: WJC 
 * @Date: 2018-12-19 18:26:36 
 * @Last Modified by: WJC
 * @Last Modified time: 2018-12-27 11:46:43
 */

/**
 * 唯一的全局变量，声明名字空间 
 */ 
var navbar = {};

navbar.loginStatus = false;

/**
 * 切换显示货币功能
 */
(function () {
    var currency = $("#wjc-nav-currency");

    // 为货币选择菜单绑定click事件
    $(".my-nav-dropdown-menu1 a:not(.my-nav-dropdown-menu1-title)").click(
        function () {
            currency.html($(this).html());
        }
    );
})();

/**
 * 不同登录状态显示不同导航菜单
 */
 navbar.changeNav = function () {
    var loginPrev = $(".wjc-login-prev");
    var loginAfter = $(".wjc-login-after");

    if (navbar.loginStatus) {
        loginPrev.removeClass("show");
        loginPrev.addClass("hidden");
        loginAfter.removeClass("hidden");
        loginAfter.addClass("show");
    } else {
        loginPrev.removeClass("hidden");
        loginPrev.addClass("show");
        loginAfter.removeClass("show");
        loginAfter.addClass("hidden");
    }
};

/**
 * 检查cookie的登录状态
 */
(function () {
    var cookie = document.cookie;
    if (cookie != "") {
        var arr1 = cookie.split("; ");
        var kv = {};
        for (var i = 0; i < arr1.length; i++) {
            var key = arr1[i].slice(0, arr1[i].indexOf("="));
            var value =  arr1[i].slice(arr1[i].indexOf("=")+1, arr1[i].length);
            
            if (key == "Userid") {
                kv.key = key;
                kv.value = value;
                break;
            }
        }
        
        $.ajax({
            type: "post",
            url: "cookieLogin.do",
            data: {
                userid: kv.value
            },
            success: function (json) {
               
                $("#wjc-user-icon").attr("src", "/airbnb/img/"+json.photo_url);
                console.log(json.photo_url);
                navbar.loginStatus = true;
                navbar.changeNav();
            }
        });
    }
})();

/**
 * 登录按钮绑定事件
 */
$("#wjc-btn-login").click (function () {
    var lname = $("#wjc-login-lname").val();
    var lpass = $("#wjc-login-lpass").val();
    var rememberme = $("#wjc-login-rememberme").prop("checked");

    $.ajax({
        type: "post",
        url: "login.do",
        async: false,
        data: {
            lname: lname,
            lpass: lpass,
            rememberme: rememberme
        },
        dataType: "text",
        error: function () {
            alert("ajax请求失败");
        },
        success: function (response) {
            if (response != "") {
                var json = eval("(" + response + ")");
//                $("#wjc-user-icon").attr("src", json.photo_url);
                $("#wjc-user-icon").attr("src", "/airbnb/img/"+json.photo_url);
                navbar.loginStatus = true;
                navbar.changeNav();
            } else {
                alert("用户名或密码错误");
            }
        }
    });

    if (navbar.loginStatus) {
        // 关闭模态框
        $('#myModal').modal('hide')
    }
});

/**
 * 登出按钮绑定事件
 */
$("#wjc-login-out").click (function () {
    $.ajax({
        type: "post",
        url: "loginOut.do",
        async: false,
        error: function () {
            alert("ajax请求失败");
        },
        success: function (response) {
            if (response == 1) {
                navbar.loginStatus = false; 
                navbar.changeNav();
            }
        }
    });
});
